﻿using Engine;
using Game;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CreatorWandModAPI
{
    public class FillingDialog : InterfaceDialog
    {
        public bool typeBool = true;

        private readonly ButtonWidget pillingButton;

        private readonly ButtonWidget pilling2Button;

        public FillingDialog(CreatorAPI creatorAPI)
            : base(creatorAPI)
        {
            XElement node = ContentManager.Get<XElement>("Dialog/Fill");
            LoadChildren(this, node);
            GeneralSet();
            setShaftXYZ();

            Y_Shaft.Text = CreatorAPI.Language switch
            {
                "zh_CN" => "正Y轴",
                "en_US" => "Y-axis",
                _ => "正Y轴"
            };

            pillingButton = Children.Find<ButtonWidget>("填充");
            pilling2Button = Children.Find<ButtonWidget>("填充2");
            LoadProperties(this, node);
        }

        public override void Update()
        {
            base.Update();
            if (pillingButton.IsClicked)
            {
                FillingBlock();
                DialogsManager.HideDialog(this);
            }

            if (pilling2Button.IsClicked)
            {
                FillingBlock(limit: true);
                DialogsManager.HideDialog(this);
            }

            upDataButton();
        }

        public void FillingBlock(bool limit = false)
        {
            Point3 Start = creatorAPI.Position[0];
            Point3 End = creatorAPI.Position[1];
            CreatorMain.Math.StartEnd(ref Start, ref End);
            if (createType == CreatorMain.CreateType.X)
            {
                (Start.X, Start.Y) = (Start.Y, Start.X);
                (End.X, End.Y) = (End.Y, End.X);
            }
            else if (createType == CreatorMain.CreateType.Z)
            {
                (Start.Z, Start.Y) = (Start.Y, Start.Z);
                (End.Z, End.Y) = (End.Y, End.Z);
            }

            Task.Run(delegate
            {
                int num = 0;
                ChunkData chunkData = new ChunkData(creatorAPI);
                creatorAPI.revokeData = new ChunkData(creatorAPI);
                for (int i = End.X; i <= Start.X; i++)
                {
                    for (int j = End.Z; j <= Start.Z; j++)
                    {
                        bool flag = false;
                        bool flag2 = false;
                        for (int k = End.Y; k <= Start.Y; k++)
                        {
                            if (!creatorAPI.launch)
                            {
                                return;
                            }

                            int num2 = (createType != CreatorMain.CreateType.Y) ? (typeBool ? k : (Start.Y + End.Y - k)) : (typeBool ? (Start.Y + End.Y - k) : k);
                            int num3 = Terrain.ExtractContents((createType == CreatorMain.CreateType.X) ? subsystemTerrain.Terrain.GetCellValueFast(num2, i, j) : ((createType != CreatorMain.CreateType.Y) ? subsystemTerrain.Terrain.GetCellValueFast(i, j, num2) : subsystemTerrain.Terrain.GetCellValueFast(i, num2, j)));
                            if (flag2 && limit && num3 != 0)
                            {
                                break;
                            }

                            if (!flag && num3 != 0)
                            {
                                flag = true;
                            }
                            else if (flag && num3 == 0)
                            {
                                flag2 = true;
                                switch (createType)
                                {
                                    case CreatorMain.CreateType.X:
                                        creatorAPI.CreateBlock(num2, i, j, blockIconWidget.Value, chunkData);
                                        break;
                                    case CreatorMain.CreateType.Y:
                                        creatorAPI.CreateBlock(i, num2, j, blockIconWidget.Value, chunkData);
                                        break;
                                    default:
                                        creatorAPI.CreateBlock(i, j, num2, blockIconWidget.Value, chunkData);
                                        break;
                                }
                                num++;
                            }
                        }
                    }
                }

                chunkData.Render();
                switch (CreatorAPI.Language)
                {
                    case "zh_CN":
                        player.ComponentGui.DisplaySmallMessage($"操作成功，共生成{num}个方块", Color.LightYellow, blinking: true, playNotificationSound: true);
                        break;
                    case "en_US":
                        player.ComponentGui.DisplaySmallMessage($"The operation was successful, generating a total of {num} blocks", Color.LightYellow, blinking: true, playNotificationSound: true);
                        break;
                    default:
                        player.ComponentGui.DisplaySmallMessage($"操作成功，共生成{num}个方块", Color.LightYellow, blinking: true, playNotificationSound: true);
                        break;
                }
            });
        }

        public override void upDataButton(CreatorMain.CreateType createType, ButtonWidget button)
        {
            if (base.createType == createType)
            {
                if (typeBool)
                {
                    typeBool = false;
                    button.Text = CreatorAPI.Language switch
                    {
                        "zh_CN" => "负" + getTypeName(createType) + "轴",
                        "en_US" => "- " + getTypeName(createType) + "-axis",
                        _ => "负" + getTypeName(createType) + "轴"
                    };

                    button.Color = Color.Red;
                }
                else
                {
                    typeBool = true;
                    button.Text = CreatorAPI.Language switch
                    {
                        "zh_CN" => "正" + getTypeName(createType) + "轴",
                        "en_US" => "+ " + getTypeName(createType) + "-axis",
                        _ => "正" + getTypeName(createType) + "轴"
                    };

                    button.Color = Color.Green;
                }

                return;
            }

            typeBool = true;
            base.createType = createType;
            button.Text = CreatorAPI.Language switch
            {
                "zh_CN" => "正" + getTypeName(createType) + "轴",
                "en_US" => "+ " + getTypeName(createType) + "-axis",
                _ => "正" + getTypeName(createType) + "轴"
            };

            button.Color = Color.Green;
            if (X_Shaft != button)
            {
                X_Shaft.Text = CreatorAPI.Language switch
                {
                    "zh_CN" => "X轴",
                    "en_US" => "X-axis",
                    _ => "X轴"
                };

                X_Shaft.Color = Color.White;
            }

            if (Y_Shaft != button)
            {
                X_Shaft.Text = CreatorAPI.Language switch
                {
                    "zh_CN" => "Y轴",
                    "en_US" => "Y-axis",
                    _ => "Y轴"
                };

                Y_Shaft.Color = Color.White;
            }

            if (Z_Shaft != button)
            {
                X_Shaft.Text = CreatorAPI.Language switch
                {
                    "zh_CN" => "Z轴",
                    "en_US" => "Z-axis",
                    _ => "Z轴"
                };

                Z_Shaft.Color = Color.White;
            }
        }
    }
}
